package com.example.temp;

import java.util.ArrayList;
import java.util.Stack;

public class Test3 {

    public static void main(String[] args) {

    }


    public class ListNode {
        int data;
        ListNode next = null;

        ListNode(int data) {
            this.data = data;
        }
    }


    /**
     * 输入一个链表，按链表从尾到头的顺序返回一个ArrayList。
     */
    public class Solution {
        public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

            Stack<Integer> stack = new Stack();

            while (listNode != null) {
                stack.push(listNode.data);
                listNode = listNode.next;
            }

            ArrayList arrayList = new ArrayList();
            while (!stack.isEmpty()) {
                arrayList.add(stack.pop());

            }
            return arrayList;

        }
    }

    public ArrayList<Integer> printListFromTailToHeadTest(ListNode listNode){

        ArrayList<Integer> arrayList = new ArrayList<>();
        Stack<Integer> stack = new Stack();
        while (listNode!=null){
            stack.push(listNode.data);
            listNode = listNode.next;
        }

        while (!stack.isEmpty()){
            arrayList.add(stack.pop());
        }
        return arrayList;

    }
}
